package cn.edu.fudan.bdke.ncov.qa;

import cn.edu.fudan.bdke.ncov.qa.model.AbsLogicForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 问答系统的主类
 * <p>
 * 后续可能会由于解析方式的不同，构建多个不同的QA系统。因此采用了建造者模式
 *
 * @author xym
 * @since 1.0
 */
public class SemanticQa {
    private static final Logger log = LoggerFactory.getLogger(SemanticQa.class);

    private AbsQaBuilder qaBuilder;

    public SemanticQa(AbsQaBuilder qaBuilder) {
        this.qaBuilder = qaBuilder;
    }

    public void setQaBuilder(AbsQaBuilder qaBuilder) {
        this.qaBuilder = qaBuilder;
    }

    /**
     * run方法用于运行QA，返回结果
     *
     * @param question 问句
     * @return 问题答案
     */
    public List<String> run(String question) {
        log.info("输入的问句为：{}", question);
        if (this.qaBuilder != null) {
            AbsLogicForm logicForm = qaBuilder.naturalLanguage2LogicForm(question);
            log.info("问句转换为逻辑形式：{}", logicForm.toString());
            String query = qaBuilder.logicForm2Query(logicForm);
            log.info("逻辑形式转换为查询语言：{}", query);
            return qaBuilder.searchResult(query);
        }
        return new ArrayList<>();
    }
}
